import {HTTP} from '../util/http.js'

export class ClassicModel extends HTTP{
	getLatest(sCallback) {
		this.request({
			url: '/classic/latest',
			success:((res) => {
				this._setLatestIndex(res.index)
				sCallback(res)
				let key = this._getKey(res.index)
				wx.setStorageSync(key, res)
			})
		})
	}

	onGetEpsoide(index, previousOrNext, sCallback) {
		const key = previousOrNext == 'next' ? this._getKey(index + 1) :this._getKey(index - 1)
		if(!wx.getStorageSync(key)) {
			this.request({
				url: '/classic/' + index + '/' + previousOrNext,
				success:  ((res) => {
					wx.setStorageSync(this._getKey(res.index), res)
					sCallback(res)
				})
			})
		} else {
			sCallback(wx.getStorageSync(key))
		}
	}

	isFirst(index) {
		return index === 1 ? true : false
	}

	isLatest(index) {
		const latestIndex = this._getLatestIndex()
		return index == latestIndex ? true : false
	}

	_setLatestIndex(index) {
		wx.setStorageSync('latest', index);
	}

	_getLatestIndex() {
		let index = wx.getStorageSync('latest')
		return index
	}

	_getKey(index) {
		const key = 'classic-' + index
		return key
	}
}